package com.easylive.component;

import com.easylive.entity.config.AppConfig;
import com.easylive.entity.constants.Constants;
import com.easylive.entity.dto.SysSettingDto;
import com.easylive.entity.dto.TokenUserInfoDto;
import com.easylive.entity.dto.UploadingFileDto;
import com.easylive.entity.dto.VideoPlayInfoDto;
import com.easylive.entity.enums.DateTimePatternEnum;
import com.easylive.entity.po.CategoryInfo;
import com.easylive.entity.po.VideoInfoFilePost;
import com.easylive.redis.RedisUtils;
import com.easylive.utils.DateUtil;
import com.easylive.utils.StringTools;
import io.lettuce.core.RedisClient;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Component
public class RedisComponent {

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private AppConfig appConfig;

    public String saveCheckCode(String code){
        String checkCodeKey = UUID.randomUUID().toString();
        redisUtils.setex(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey,code,Constants.REDIS_KEY_EXPIRES_ONE_MINUTE  * 10);
        return checkCodeKey;
    }

    public String getCheckCode(String checkCodeKey){
        return (String)redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);

    }

    public void cleanCheckCode(String checkCodeKey) {
        redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
    }

    public void saveTokenInfo(TokenUserInfoDto tokenUserInfoDto){
        String token = UUID.randomUUID().toString();
        tokenUserInfoDto.setExpireAt(System.currentTimeMillis() + Constants.REDIS_KEY_EXPIRES_ONE_DAY * 7);
        tokenUserInfoDto.setToken(token);
        redisUtils.setex(Constants.REDIS_KEY_TOKEN_WEB + token,tokenUserInfoDto,Constants.REDIS_KEY_EXPIRES_ONE_DAY * 7);

    }
    public void updateTokenInfo(TokenUserInfoDto tokenUserInfoDto) {
        redisUtils.setex(Constants.REDIS_KEY_TOKEN_WEB + tokenUserInfoDto.getToken(),tokenUserInfoDto,Constants.REDIS_KEY_EXPIRES_ONE_DAY * 7);

    }

    public TokenUserInfoDto getTokenInfo(String token){
        return (TokenUserInfoDto) redisUtils.get(Constants.REDIS_KEY_TOKEN_WEB + token);

    }

    public void cleanTokenInfo(String token) {
        redisUtils.delete(Constants.REDIS_KEY_TOKEN_WEB + token);
    }

    public String saveTokenInfo4Admin(String account){
        String token = UUID.randomUUID().toString();
        redisUtils.setex(Constants.REDIS_KEY_TOKEN_ADMIN + token,account,Constants.REDIS_KEY_EXPIRES_ONE_DAY);
        return token;


    }

    public void cleanTokenInfo4Admin(String token) {
        redisUtils.delete(Constants.REDIS_KEY_TOKEN_ADMIN + token);
    }

    public String getTokenInfo4Admin(String token){
        return (String) redisUtils.get(Constants.REDIS_KEY_TOKEN_ADMIN + token);

    }

    public void saveCategoryList(List<CategoryInfo> categoryInfoList){
        redisUtils.set(Constants.REDIS_KEY_CATEGORY_LIST,categoryInfoList);

    }

    public List<CategoryInfo> getCategoryList() {
        return (List<CategoryInfo>) redisUtils.get(Constants.REDIS_KEY_CATEGORY_LIST);
    }

    public String savePreVideoFileInfo(String userId,String fileName,Integer chunks){
        String uploadId = StringTools.getRandomString(Constants.Length_15);
        UploadingFileDto uploadingFileDto = new UploadingFileDto();
        uploadingFileDto.setChunks(chunks);
        uploadingFileDto.setFileName(fileName);
        uploadingFileDto.setUploadId(uploadId);
        uploadingFileDto.setChunkIndex(0);
        String day = DateUtil.format(new Date(), DateTimePatternEnum.YYYYMMDD.getPattern());
        String filePath = day + "/" + userId + uploadId;
        String folder = appConfig.getProjectFolder() + Constants.FILE_FOLDER + Constants.FILE_TEMP + filePath;
        File folderFile = new File(folder);
        if(!folderFile.exists()){
            folderFile.mkdirs();
        }
        uploadingFileDto.setFilePath(filePath);
        redisUtils.setex(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId,uploadingFileDto,Constants.REDIS_KEY_EXPIRES_ONE_DAY);
        return uploadId;

    }

    public UploadingFileDto getUploadingFileDto(String userId,String uploadId){
        return (UploadingFileDto) redisUtils.get(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);

    }

    public SysSettingDto getSysSettingDto(){
        SysSettingDto sysSettingDto = (SysSettingDto)redisUtils.get(Constants.REDIS_KEY_SYS_SETTING);
        if(sysSettingDto == null){
            sysSettingDto = new SysSettingDto();
        }
        return sysSettingDto;

    }

    public void updateVideoFileInfo(String userId, UploadingFileDto uploadingFileDto){
        redisUtils.setex(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadingFileDto.getUploadId(),uploadingFileDto,Constants.REDIS_KEY_EXPIRES_ONE_DAY);
    }

    public void delVideoFileInfo(String userId,String uploadId){
        redisUtils.delete(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);
    }

    //消息队列
    public void addFile2DelQueue(String videoId,List<String> delFilePathList){
        redisUtils.lpushAll(Constants.REDIS_KEY_FILE_DEL + videoId,delFilePathList,Constants.REDIS_KEY_EXPIRES_ONE_DAY * 7);
    }

    public List<String> getDelFileList(String videoId){
        return redisUtils.getQueueList(Constants.REDIS_KEY_FILE_DEL + videoId);
    }

    public void cleanFileDelList(String videoId){
        redisUtils.delete(Constants.REDIS_KEY_FILE_DEL + videoId);
    }

    public void addFile2TransferQueue(List<VideoInfoFilePost> addFileList){
        redisUtils.lpushAll(Constants.REDIS_KEY_FILE_TRANSFER,addFileList,0);
    }

    public void addFile2TransferQueue4one(VideoInfoFilePost addFileList){
        redisUtils.lpush(Constants.REDIS_KEY_FILE_TRANSFER,addFileList,0L);
    }

    public VideoInfoFilePost getFileFromTransferQueue(){
        return  (VideoInfoFilePost)redisUtils.rpop(Constants.REDIS_KEY_FILE_TRANSFER);
    }

    public Integer reportVideoPlayOnline(String fileId, String deviceId){
        String userPlayOnlineKey = String.format(Constants.REDIS_KEY_VIDEO_PLAY_COUNT_USER,fileId,deviceId);

        String playOnlineCountKey = String.format(Constants.REDIS_KEY_VIDEO_PLAY_COUNT_ONLINE,fileId);

        if(!redisUtils.keyExists(userPlayOnlineKey)){
            redisUtils.setex(userPlayOnlineKey,fileId,Constants.REDIS_KEY_EXPIRES_ONE_SECOND * 8);
            return redisUtils.incrementex(playOnlineCountKey,Constants.REDIS_KEY_EXPIRES_ONE_SECOND * 10).intValue();
        }
        redisUtils.expire(playOnlineCountKey,Constants.REDIS_KEY_EXPIRES_ONE_SECOND * 10);
        redisUtils.expire(userPlayOnlineKey,Constants.REDIS_KEY_EXPIRES_ONE_SECOND * 8);
        Integer count = (Integer)redisUtils.get(playOnlineCountKey);

        return count == null ? 1 : count;

    }

    public void decrementPlayOnlineCount(String key){
        redisUtils.decrement(key);
    }

    public void addHotKeyWordCount(String keyword){
        redisUtils.zaddCount(Constants.REDIS_KEY_HOT_KEYWORD,keyword);
    }

    public List<String> getHotKeyWordList(Integer top){
        return redisUtils.getZSetList(Constants.REDIS_KEY_HOT_KEYWORD,top - 1);
    }

    public void addVideoPlay(VideoPlayInfoDto videoPlayInfoDto){
        redisUtils.lpush(Constants.REDIS_KEY_QUEUE_VIDEO_PLAY,videoPlayInfoDto,null);

    }

    public VideoPlayInfoDto getVideoPlayInfo(){
        return (VideoPlayInfoDto)redisUtils.rpop(Constants.REDIS_KEY_QUEUE_VIDEO_PLAY);
    }

    public void recordVideoPlayCount(String videoId){
        String date = DateUtil.format(new Date(),DateTimePatternEnum.YYYY_MM_DD.getPattern());
        redisUtils.incrementex(Constants.REDIS_KEY_VIDEO_PLAY_COUNT + date+":"+videoId,Constants.REDIS_KEY_EXPIRES_ONE_DAY * 2);

    }




}
